package controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import pojo.StuInfo;
import pojo.User;
import service.StuInfoService;
import utils.ResultUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jesse
 * @Date 2019/5/27 10:03
 * @Version 1.0
 */
@Controller
@RequestMapping("/stuInfo")
@Slf4j
public class StuInfoController {

    @Autowired
    private StuInfoService stuInfoService;


    @RequestMapping(value = "/gotoedit")
    public String toEdit(@RequestParam("id") String id , HttpServletRequest request){
        log.info("id="+id);
        StuInfo stuInfo = stuInfoService.getStuById(id);
//        StuInfo stuInfo = stuInfoService.getStuById(id);
        request.setAttribute("stuInfo1",stuInfo);
        request.setAttribute("id",id);
//        request.setAttribute("stuId",stuId);
        return "/WEB-INF/view/stuInfo/edit.jsp";
    }
    @GetMapping(value = "/index")
    public ModelAndView index(HttpSession session){
        User user = (User) session.getAttribute("USER_SESSION");
        session.setAttribute("username",user.getUsername());
        return new ModelAndView("/WEB-INF/view/stuInfo/stuInfo.jsp");
    }

    @RequestMapping(value = "/gotoadd",method = RequestMethod.GET)
    public String dotoadd(){
        return "/WEB-INF/view/stuInfo/add.jsp";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public String add(@Valid @RequestBody StuInfo stuInfo, HttpSession session, HttpServletRequest request){
        System.out.println(request.getParameter("stuInfo"));
        System.out.println("345454");
        Integer result=0;
        try {
            result = stuInfoService.insert(stuInfo);
        }catch (Exception e){
            e.printStackTrace();
            session.setAttribute("msg","学生信息插入失败");
            return ResultUtils.addResult(result);
        }
        log.info("result="+result);
       return ResultUtils.addResult(result);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public String change(@Valid @RequestBody StuInfo stuInfo){
        Integer result = stuInfoService.update(stuInfo);
        return ResultUtils.result(result);
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete(String id){
        Integer result = stuInfoService.delete(id);

        return ResultUtils.result(result);
    }

    @RequestMapping(value = "/getOne",method = RequestMethod.POST)
    @ResponseBody
    public List<StuInfo> getOne(@RequestParam("id") String id){
        if (id == null) {
            return stuInfoService.getAllStu();
        }
        StuInfo stuInfo = stuInfoService.getStuById(id);
        List<StuInfo> stuInfoList = new ArrayList<>();
        stuInfoList.add(stuInfo);
        return stuInfoList;
    }

    @RequestMapping(value = "/getByClazz",method = RequestMethod.POST)
    @ResponseBody
    public List<StuInfo> getByClazz(Integer clazz){
        System.out.println(clazz);
        List<StuInfo> stuInfos = stuInfoService.getStuByClazz(clazz);
        return stuInfos;
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.POST)
    @ResponseBody
    public List<StuInfo> getAll(){
        List<StuInfo> stuInfos = stuInfoService.getAllStu();
        return stuInfos;
    }


    @RequestMapping("/error")
    public String errors(String result,String url,HttpServletRequest request){
        System.out.println(result);
        System.out.println(url);
        request.setAttribute("result",result);
        request.setAttribute("url",url);
        return "/WEB-INF/view/common/error.jsp";
    }

    @RequestMapping("/success")
    public String success1(String result,String url,HttpServletRequest request){
        System.out.println(result);
        System.out.println(url);
        request.setAttribute("result",result);
        request.setAttribute("url",url);
        return "/WEB-INF/view/common/success.jsp";
    }

    @RequestMapping(value = "/getByArray")
    @ResponseBody
    public List<StuInfo> getByArray(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                    @RequestParam(value = "size",defaultValue = "5") Integer size){
        List<StuInfo> stuInfoList =  stuInfoService.getStuByArray(currentPage, size);
//        map.put("number",userFeedbackDTOPage.getTotalElements());
        return stuInfoList;
    }

    @RequestMapping(value = "/getNumber",method = RequestMethod.POST)
    @ResponseBody
    public Integer getNumber(){
        List<StuInfo> stuInfos = stuInfoService.getAllStu();
        return stuInfos.size();
    }
}
